<?php
App::uses('AppHelper', 'View/Helper');

class TioCachasHelper extends AppHelper {

    /**
     * Agregamos los JS y CSS necesarios para Kendo
     */
    public function addKendo($kendoTheme) {
        $this->_View->Html->script('vendors/kendo/kendo.all.min', array('block' => 'scriptBottom'));
        $this->_View->Html->css('vendors/kendo/kendo.common.min', array('block' => 'stylesTop'));
        $this->_View->Html->css('vendors/kendo/'.$kendoTheme.'.min', array('block' => 'stylesTop'));
        $this->_View->Html->css('vendors/kendo/kendo.dataviz.min', array('block' => 'stylesTop'));
        $this->_View->Html->css('vendors/kendo/kendo.dataviz.bootstrap.min', array(
            'block' => 'stylesTop'));
        $this->_View->Html->script('vendors/kendo/kendo.timezones.min', array('block' => 'scriptBottom'));
        $this->_View->Html->script('vendors/kendo.es-MX', array('block' => 'scriptBottom'));
        $this->_View->Html->script('vendors/kendo/pako_deflate.min', array('block' => 'scriptBottom'));
        $this->_View->Html->script('vendors/kendo/jszip.min', array('block' => 'scriptBottom'));
        $cultures = array('es-MX');
        $t = $this;
        array_walk($cultures, function($culture)use($t) {
            $t->_View->Html->script('vendors/kendo/cultures/kendo.culture.' . $culture . '.min', array(
                'block' => 'scriptBottom'));
        });
    }

    public function templateClassSwig($element, $otherClass = null) {
        ?>
        <script type="text/template" class="template <?php echo $otherClass; ?>">
        <?php echo $this->_View->element('Swig/' . $element); ?>
        </script>
        <?php
    }

    public function templateSwig($id, $element) {
        ?>
        <script type="text/template" id="<?php echo $id; ?>">
        <?php echo $this->_View->element('Swig/' . $element); ?>
        </script>
        <?php
    }

    public function addJsBackbone($model, $includeView = true, $includeModel = true, $includeController = true) {
        if ($includeModel === true) {
            $this->_View->Html->script('backbone/models/model-' . $model, array(
                'block' => 'scriptBottom'));
        }
        if ($includeController === true) {
            $this->_View->Html->script('backbone/collections/collection-' . $model, array(
                'block' => 'scriptBottom'));
        }
        if ($includeView === true) {
            $this->_View->Html->script('backbone/views/view-' . $model, array(
                'block' => 'scriptBottom'));
        }
    }

    /**
     * Generamos un arreglo con URL hacia acciones CRUD contenidas en el mismo 
     * URL para CREATE
     * URL para READ
     * URL para UPDATE
     * URL para DELETE
     * URL para Preguntas
     * controlador.
     * @return array
     */
    public function urlsCRUD() {
        $args = func_get_args();
        $urlCreate = $this->_View->Html->url(array('action' => 'create'));
        $read = array_merge(array('action' => 'read'), $args);
        $urlRead = $this->_View->Html->url($read);
        $urlUpdate = $this->_View->Html->url(array('action' => 'update'));
        $urlDelete = $this->_View->Html->url(array('action' => 'destroy'));
        $urlQuestions = $this->_View->Html->url(array('controller' => 'Questions', 'action' => 'admin'));
        $urlAnswers = $this->_View->Html->url(array('controller' => 'Answers', 'action' => 'admin'));
        $array = array(
            'create' => $urlCreate,
            'read' => $urlRead,
            'update' => $urlUpdate,
            'destroy' => $urlDelete,
            'questions' => $urlQuestions,
            'answer' => $urlAnswers
        );
        return $array;
    }

    public function breadcrumb($array) {
        if (is_array($array) === true && count($array) > 0):
            ?>
            <ol class="breadcrumb">
                <?php foreach ($array as $key => $element): ?>
                    <li>
                        <a href="<?php echo $element; ?>">
                            <?php echo $key; ?>
                        </a>
                    </li>
                <?php endforeach; ?>
            </ol>
        <?php endif; ?>
        <?php
    }

    public function addWarning() {
        ?>
        <div class="alert alert-warning">
            <p>
                <?php echo __('La modificación de este catálogo puede alterar el funcionamiento del sistema.
                Solamente usuarios autorizados deben modificar este catálogo, recuerde que
                todo cambio, modificación o alteración queda registrado'); ?>
            </p>
        </div>
        <?php
    }

    /**
     * 
     * @param mixed $url String or Array
     */
    public function back($url, $text = 'Regresar') {
        ?>
        <div style="margin-bottom: 15px;">
            <a class="btn btn-default" href="<?php echo $this->_View->Html->url($url); ?>">
                <?php echo $text; ?>
            </a>
        </div>
        <?php
    }

    public function headerProcedure($procedure) {
        $d1 = new DateTime($procedure['date_1']);
        $date1 = $d1->format('d.M.Y');
        $d2 = new DateTime($procedure['date_2']);
        $date2 = $d2->format('d.M.Y');
        ?>
        <table class='table table-bordered table-condensed'>
            <tr>
                <td>
                    <?php echo $this->_View->Html->image($procedure['logo'], array('alt' => 'Bosch')); ?>
                </td>
                <td><strong><?php echo $procedure['title']; ?></strong></td>
                <td><?php echo $procedure['edition']; ?></td>
                <td><?php echo __('Pagina 1/1'); ?></td>
                <td class='dates'>
                    <?php echo __('Fecha emisión'); ?>
                    <br/>
                    <?php echo $date1; ?>
                    <br/>
                    <?php echo __('Fecha revisión'); ?>
                    <br/>
                    <?php echo $date2; ?>
                    <br/>
                </td>
            </tr>
            <tr>
                <td><?php echo $procedure['depto']; ?></td>
                <td><strong><?php echo $procedure['description']; ?></strong></td>
                <td colspan="2">
                    <?php echo __('Autor'); ?>
                    <?php echo $procedure['author']; ?>
                </td>
                <td>
                    <?php echo __('Teléfono'); ?>
                    <?php echo $procedure['phone']; ?>
                </td>
            </tr>
        </table>
        <?php
    }

}
